package ttOrder.controller;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ttOrder.model.po.DinnerTable;
import ttOrder.model.po.Food;
import ttOrder.service.DinnerTableServiceImpl;
import ttOrder.service.FoodServiceImpl;
import ttOrder.service.OrderServiceImpl;
import ttOrder.util.CommonMessageResultVO;

@Controller
@RequestMapping("/ttOrder")
public class dinerTableController {
	
	@Resource
	private DinnerTableServiceImpl dinnerTableService;
	@Resource
	private FoodServiceImpl foodService;
	@Resource
    private OrderServiceImpl orderService;
	
		
	/**************↑前端流程  --- 餐桌管理↓***************/
	//展示餐桌
	@ResponseBody
	@RequestMapping("/dinnerTables")
	public List<DinnerTable> dinnerTables(Model model){
		List<DinnerTable> DinnerTable = dinnerTableService.getAll();
		model.addAttribute("DinnerTable", DinnerTable);
		return DinnerTable;
	}
	
	
	//添加餐桌
	@ResponseBody
	@RequestMapping(value="/addTable",method=RequestMethod.POST)
	public CommonMessageResultVO addTable(Model model,DinnerTable dinnerTable){
		CommonMessageResultVO vo = new CommonMessageResultVO();
		try {
			dinnerTableService.insert(dinnerTable);
			vo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}
	
	//修改餐桌
		@ResponseBody
		@RequestMapping(value="/editTable",method=RequestMethod.POST)
		public CommonMessageResultVO editTable(Model model,DinnerTable dinnerTable){
			CommonMessageResultVO resultvo = new CommonMessageResultVO();
			try {
				dinnerTableService.update(dinnerTable);
				resultvo.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return resultvo;
		}
	
	//预定餐桌&取消预定
	@ResponseBody
	@RequestMapping(value="/bookTable",method=RequestMethod.POST)
	public CommonMessageResultVO bookTable(Model model,int[] id,int tableStatus){
		//Utils.toPrintObj(dinnerTable);
		CommonMessageResultVO resultvo = new CommonMessageResultVO();
		try {
			for(int i = 0 ; i<id.length ; i++){
				DinnerTable dt = new DinnerTable();
				dt.setId(id[i]);
				DinnerTable bookdt = dinnerTableService.getByKey(dt);
				
				bookdt.setTableStatus(tableStatus);
				dinnerTableService.update(bookdt);
			}
			resultvo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultvo;
	}
	
	//删除餐桌
	@ResponseBody
	@RequestMapping(value="/delTable",method=RequestMethod.POST)
	public CommonMessageResultVO delTable(Model model,int[] ids){
		CommonMessageResultVO resultvo = new CommonMessageResultVO();
		try {
			for(int i = 0 ; i<ids.length ; i++){
				DinnerTable dt = new DinnerTable();
				dt.setId(ids[i]);
				dinnerTableService.deleteByKey(dt);
			}
			resultvo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultvo;
	}
	
	
	

}
